package org.feng.utils.redis;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author by feng
 * @des redisHash
 * @date 2021/4/27 14:03
 */
@Component
public class RedisHash extends RedisCommon {
    /**
     * 获取redis hash下的value
     *
     * @param key
     * @param keyTwo
     * @return
     */
    public Object get(Object key, Object keyTwo) {
        return redisTemplate.opsForHash().get(key, keyTwo);
    }

    /**
     * 添加数据
     *
     * @param key
     * @param keyTwo
     * @param value
     */
    public RedisTemplate<Object, Object> put(Object key, Object keyTwo, Object value) {
        redisTemplate.opsForHash().put(key, keyTwo, value);
        return super.redisTemplate;
    }

    /**
     * 删除 redis下 hash的键
     *
     * @param key
     * @param keys
     */
    public void delete(Object key, Object... keys) {
        redisTemplate.opsForHash().delete(key, keys);
    }

    /**
     * 添加多个
     *
     * @param key
     * @param map
     */
    public RedisTemplate<Object, Object> putAll(Object key, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
        return super.redisTemplate;
    }

    /**
     * 获取多个
     *
     * @param key
     * @param keys
     * @return
     */
    public List<Object> multiGet(Object key, List<Object> keys) {
        return redisTemplate.opsForHash().multiGet(key, keys);
    }

    /**
     * 获取所有
     *
     * @param key
     * @return
     */
    public Map<Object, Object> entries(Object key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 判断key
     *
     * @param key
     * @param keyTwo
     * @return
     */
    public Boolean hasKeyHash(Object key, Object keyTwo) {
        return redisTemplate.opsForHash().hasKey(key, keyTwo);
    }

    /**
     * 自增
     *
     * @param key
     * @param keyTow
     * @param value
     * @return
     */
    public Long incrementHashLong(Object key, Object keyTow, Long value) {
        return redisTemplate.opsForHash().increment(key, keyTow, value);
    }

    /**
     * 自增
     *
     * @param key
     * @param keyTow
     * @param value
     * @return
     */
    public Double incrementHashDouble(Object key, Object keyTow, Double value) {
        return redisTemplate.opsForHash().increment(key, keyTow, value);
    }

    /**
     * 获取 redis 下所有的hash键
     *
     * @param key
     * @return
     */
    public Set<Object> keysHash(Object key) {
        return redisTemplate.opsForHash().keys(key);
    }

    /**
     * 获取redis hash键下的所有值
     *
     * @param key
     * @return
     */
    public List<Object> valuesHash(Object key) {
        return redisTemplate.opsForHash().values(key);
    }
}

